//
//  AppDelegate+DTCAppService.m
//  BluetoothProject
//
//  Created by Kendama Chen on 17/8/16.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import <Bugly/Bugly.h>
#import "IQKeyboardManager.h"
#import "AppDelegate+DTCAppService.h"
#import "BlueToothDataHandle.h"
#import "DTCReConnectBlueManager.h"

//#import "Reachability.h"


@interface AppDelegate ()<DTCBluetoothManagerDelegate>

@end

@implementation AppDelegate (DTCAppService)

- (void)setNetworkBaseConfig
{
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
    config.baseUrl = ServerURL;
}

- (void)setBuglyConfig
{
    [Bugly startWithAppId:@"a4f10f09bb"];
}

- (void)setWindowBaseConfig
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [[IQKeyboardManager sharedManager]setEnable:YES];
    [[IQKeyboardManager sharedManager]setKeyboardDistanceFromTextField:100];
    
    [SVProgressHUD setAnimationDuration:2.0];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
}


/**
 *  蓝牙配置设置
 */
- (void)setBluetoothManagerConfig
{

    [DTCBluetoothManager shareInstance].delegate = self;
    [[DTCBluetoothManager shareInstance] setWriteNotifyCharacteristicValue:@[@"F000EFE3-0451-4000-0000-00000000B000"]];//设置进行notify的通道，不设置的话默认所有包含notify的特征值都进行广播
//    [[DTCBluetoothManager shareInstance] setReadNotifyCharacteristicValue:@[@"1002"]];//设置进行read的通道，不设置的话默认所有包含read的特征值进行广播
    [[DTCBluetoothManager shareInstance] setFilterDeviceByServiceUUID:@[@"F000EFE0-0451-4000-0000-00000000B000"] onDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) {
        //        NSData *kCBAdvDataManufacturerData = [advertisementData valueForKey:@"kCBAdvDataManufacturerData"];
        //        if (([peripheralName rangeOfString:@"ICSKY"].location != NSNotFound && kCBAdvDataManufacturerData)) {
        //            return YES;
        //        }
        //        return NO;
        if (([peripheralName rangeOfString:@"HB016"].location != NSNotFound && peripheralName.length!= 0)) {
            return YES;
        }
        return NO;
        
        
    }];//设备过滤条件
}

#pragma mark - 代理
#pragma mark - <DTCBluetoothManagerDelegate>
/*
 当前中心的状态
 */
- (void)dtcBluetoothManagerCentralManagerState:(CBManagerState)state
{
    if (state == CBManagerStatePoweredOff)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:DeviceDisconnect object:nil];
    }
}

/*
 设备连接成功
 */
- (void)dtcBluetoothManagerConnectSuccessByDevice:(DTCDeviceModel *)deviceModel
{
//    [SVProgressHUD showSuccessWithStatus:MyNSLocalizedString(@"连接设备成功")];
    [[NSNotificationCenter defaultCenter] postNotificationName:DeviceConnectSuccess object:nil];
    SAVE_OBJECT(deviceModel.mac, MAC);
}



/*
 设备连接断开
 */
- (void)dtcBluetoothManagerDisConnectByDevice:(DTCDeviceModel *)deviceModel
{
    [[NSNotificationCenter defaultCenter] postNotificationName:DeviceDisconnect object:nil];
    [[DTCBluetoothManager shareInstance] scanAroundDevice];
//    [SVProgressHUD showErrorWithStatus:MyNSLocalizedString(@"连接设备失败")];
}

/**
 设备连接失败
 */
- (void)dtcBluetoothManagerConnectFailByDevice:(DTCDeviceModel *)deviceModel error:(NSError *)error
{
    
}

/**
 发现特征
 */
- (void)dtcBluetoothManagerDiscoverCharacteristicSerice:(DTCDeviceModel *)deviceModel characeristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    
}



/*
 设备notify数据返回
 */
- (void)dtcBluetoothManagerWriteDataBack:(NSData *)data andNotifyCharacteristic:(NSString *)characterValue sendByDevice:(DTCDeviceModel *)deviceModel
{
     [[BlueToothDataHandle sharedInstance] notifyBlueToothWireteDataBack:data sendeByDevice:deviceModel];
}

/*
 设备read数据返回
 */
- (void)dtcBluetoothManagerReadDataBack:(NSData *)data andNotifyCharacteristic:(NSString *)characterValue sendByDevice:(DTCDeviceModel *)deviceModel
{
   
}

/*
 设备列表发生改变的时候返回
 */
- (void)dtcBluetoothManagerSearchDeviceChange:(NSArray *)deviceList
{
    for (DTCDeviceModel *device in deviceList) {
        if ([device.mac isEqualToString:EXRACT_OBJECT(DEVICEMAC)]) {
            [[DTCBluetoothManager shareInstance] connectDevice:device];
            [[DTCReConnectBlueManager sharedInstance] startReconnectDevice:device];
        }
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:SearchDeviceList object:nil];
}

- (void)setNetworkMonitorConfig
{
    //    self.hostReach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    //
    //    self.hostReach.reachableBlock = ^(Reachability * reachability)
    //    {
    //        NSLog(@"========%@",[NSString getNetWorkStates]);
    //        NSString * temp = [NSString stringWithFormat:@"BAIDU Block Says Reachable(%@)", reachability.currentReachabilityString];
    //        NSLog(@"%@", temp);
    //        if (reachability.currentReachabilityStatus == ReachableViaWiFi){
    //
    //        }else{
    //            [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFY_NET_CHANGE object:nil];
    //        }
    //    };
    //
    //    self.hostReach.unreachableBlock = ^(Reachability * reachability)
    //    {
    //        if (![NSString getDeviceSSID]) {
    //            [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFY_NET_CHANGE object:nil];
    //        }
    //    };
    //    [self.hostReach startNotifier];
}

@end
